package com.swm.resource;

import java.util.List;

import javax.jdo.annotations.Persistent;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.swm.model.User;
import com.swm.service.MemberService;

/**
 * @author shk
 *
 */
@Path("/member")
@Component
@Singleton
public class MemberResource {
	//public static Logger logger;	
	@Autowired
	private MemberService memberService = new MemberService();

	//public static final Log logger = LogFactory.getLog(MemberResource.class);
	public MemberResource() {
		//logger = Logger.getRootLogger();
		//logger.info( "[CREATE] UserService" );
	}
	
	/**
	 * Join Memeber
	 * @param user
	 */
	@POST
    @Consumes("application/xml")
    public void addUser( User user ) {
    	memberService.AddUser( user );
    }
	
	/**
	 * Request UserList
	 * @return Result
	 */
	@GET
	@Produces("application/xml")
	public List<User> getUsers() {
		//logger.info( "[REQ] USER-LIST");
		List<User> users = memberService.getAllUser();
		return users;
	}
	
	/**
	 * Login
	 * @param user
	 * @return
	 */
	@POST // GET
	@Path("/login")
    @Consumes("application/xml")
    @Produces("text/plain")
    public String getLogin( User user ) {
		//logger.info( "[REQ] LOGIN : ID = " + user.getId() + ", Pass = " + user.getPassword() );
		if ( memberService.login( user ))
			return "Login success";
		
		return "Login fail";
    }

	/**
	 * Check Duplicated ID
	 * @param userID
	 * @return
	 */
	@GET
	@Path("/check/{ID}")
	@Produces("application/xml")
	public User getUser(@PathParam("ID") String userID) {
		//logger.info( "[REQ] Duplicated : ID = " + userID );
		User User = memberService.getUser( userID );		
		return User;
	}

	/*
    @DELETE
    @Path("/{ID}")
    public void deleteUser(@PathParam("ID") String userID) {
    	System.out.println("== [ENTRY] delete Greeting==");    	
    	
    	this.memberService.DeleteUser(userID);
    	
    	System.out.println("== [OUT] delete Greeting==");    	
    }
   */

/*
	@PUT
    @Consumes("application/xml")
    public void updateUser(User user) {
    	System.out.println("== [ENTRY] update User ==");
    	if (user != null) {
    		System.out.println( user.getId() );	
    		memberService.UpdateUser(user);
    	}
    	System.out.println("== [OUT] update User ==");
    }
 */   

}